package com.qf.school.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.school.entity.Student;
import com.qf.school.mapper.StudentMapper;
import com.qf.school.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void insert(Student student) {
        int i = studentMapper.insert(student);
        if(i!=1){
            System.out.println("【添加学生信息】添加失败！");
            throw new RuntimeException("【添加学生信息】添加失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateById(Student student) {
        int i = studentMapper.updateByPrimaryKeySelective(student);
        if(i!=1){
            System.out.println("【修改学生信息】修改失败！");
            throw new RuntimeException("【修改学生信息】修改失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Long id) {
        int i = studentMapper.deleteByPrimaryKey(id);
        if(i!=1){
            System.out.println("【删除学生信息】删除失败！");
            throw new RuntimeException("【删除学生信息】删除失败！");
        }
    }

    @Override
    public PageInfo<Student> findAllByPage(Integer page, Integer rows) {
        //1、分页
        PageHelper.startPage(page, rows);

        //2、查询
        List<Student> studentList=studentMapper.findAllByPage();

        //3、封装PageInfo
        PageInfo<Student> pageInfo = new PageInfo<>(studentList);

        //4、返回
        return pageInfo;
    }
}
